{% extends 'base.html' %}

{% block title %}{% if consumable %}编辑耗材{% else %}新增耗材{% endif %} - 固定资产管理系统{% endblock %}

{% block content %}
<div class="row">
    <div class="col-md-8">
        <h2>{% if consumable %}编辑耗材{% else %}新增耗材{% endif %}</h2>
        
        <form method="post">
            {% csrf_token %}
            
            <div class="row">
                <div class="col-md-6">
                    <div class="mb-3">
                        <label for="name" class="form-label">耗材名称 *</label>
                        <input type="text" class="form-control" id="name" name="name" value="{% if consumable %}{{ consumable.name }}{% else %}{{ name|default:'' }}{% endif %}" required>
                    </div>
                </div>
                <div class="col-md-6">
                    <div class="mb-3">
                        <label for="code" class="form-label">耗材编码 *</label>
                        <input type="text" class="form-control" id="code" name="code" value="{% if consumable %}{{ consumable.code }}{% else %}{{ code|default:'' }}{% endif %}" required>
                    </div>
                </div>
            </div>
            
            <div class="row">
                <div class="col-md-6">
                    <div class="mb-3">
                        <label for="category" class="form-label">耗材类别 *</label>
                        <select class="form-select" id="category" name="category" required>
                            <option value="">请选择耗材类别</option>
                            {% for category in categories %}
                                <option value="{{ category.id }}" {% if consumable and consumable.category_id == category.id or category_id|add:"0" == category.id %}selected{% endif %}>
                                    {{ category.name }}
                                </option>
                            {% endfor %}
                        </select>
                    </div>
                </div>
                <div class="col-md-6">
                    <div class="mb-3">
                        <label for="warehouse" class="form-label">所属仓库 *</label>
                        <select class="form-select" id="warehouse" name="warehouse" required>
                            <option value="">请选择仓库</option>
                            {% for warehouse in warehouses %}
                                <option value="{{ warehouse.id }}" {% if consumable and consumable.warehouse_id == warehouse.id or warehouse_id|add:"0" == warehouse.id %}selected{% endif %}>
                                    {{ warehouse.name }}
                                </option>
                            {% endfor %}
                        </select>
                    </div>
                </div>
            </div>
            
            <div class="row">
                <div class="col-md-6">
                    <div class="mb-3">
                        <label for="unit" class="form-label">单位 *</label>
                        <input type="text" class="form-control" id="unit" name="unit" value="{% if consumable %}{{ consumable.unit }}{% else %}{{ unit|default:'' }}{% endif %}" required>
                    </div>
                </div>
                <div class="col-md-6">
                    <div class="mb-3">
                        <label for="quantity" class="form-label">数量 *</label>
                        <input type="number" class="form-control" id="quantity" name="quantity" step="0.01" value="{% if consumable %}{{ consumable.quantity }}{% else %}{{ quantity|default:'' }}{% endif %}" required>
                    </div>
                </div>
            </div>
            
            <div class="row">
                <div class="col-md-6">
                    <div class="mb-3">
                        <label for="min_quantity" class="form-label">最低库存</label>
                        <input type="number" class="form-control" id="min_quantity" name="min_quantity" step="0.01" value="{% if consumable %}{{ consumable.min_quantity }}{% else %}{{ min_quantity|default:'' }}{% endif %}">
                    </div>
                </div>
                <div class="col-md-6">
                    <div class="mb-3">
                        <label for="purchase_price" class="form-label">采购单价</label>
                        <input type="number" class="form-control" id="purchase_price" name="purchase_price" step="0.01" value="{% if consumable %}{{ consumable.purchase_price }}{% else %}{{ purchase_price|default:'' }}{% endif %}">
                    </div>
                </div>
            </div>
            
            <div class="row">
                <div class="col-md-6">
                    <div class="mb-3">
                        <label for="supplier" class="form-label">供应商</label>
                        <select class="form-select" id="supplier" name="supplier">
                            <option value="">请选择供应商</option>
                            {% for supplier in suppliers %}
                                <option value="{{ supplier.id }}" {% if consumable and consumable.supplier_id == supplier.id or supplier_id|add:"0" == supplier.id %}selected{% endif %}>
                                    {{ supplier.name }}
                                </option>
                            {% endfor %}
                        </select>
                    </div>
                </div>
            </div>
            
            <div class="mb-3">
                <label for="description" class="form-label">描述</label>
                <textarea class="form-control" id="description" name="description" rows="3">{% if consumable %}{{ consumable.description }}{% else %}{{ description|default:'' }}{% endif %}</textarea>
            </div>
            
            <div class="mb-3">
                <button type="submit" class="btn btn-primary">{% if consumable %}更新{% else %}创建{% endif %}</button>
                <a href="{% url 'assets:consumable_list' %}" class="btn btn-secondary">取消</a>
            </div>
        </form>
    </div>
</div>
{% endblock %}